package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.FacilityInfoMapper;
import com.atguigu.lease.web.admin.mapper.FeeValueMapper;
import com.atguigu.lease.web.admin.mapper.LabelInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    //如果写sql使用Mapper  其它都使用Service
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;

    /**
     * 分析:
     *      1.如果用户有id 表示修改操作 先删除所有的中间表数据 之后再次新增
     * @param apartmentSubmitVo
     */
    @Override
    @Transactional
    public void saveOrUpdateItem(ApartmentSubmitVo apartmentSubmitVo) {
        //1.获取id号
        if(apartmentSubmitVo.getId() !=null){
            //1.1删除图片信息.
            LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoQueryWrapper);

            //1.2 删除公寓配套信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

            //1.3删除公寓和标签
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelQueryWrapper);

            //1.4删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
        }


        //根据省市轻区的id查询对应的名称
        Long provinceId = apartmentSubmitVo.getProvinceId();
        ProvinceInfo provinceInfo = provinceInfoService.getById(provinceId);
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        Long cityId = apartmentSubmitVo.getCityId();
        CityInfo cityInfo = cityInfoService.getById(cityId);
        apartmentSubmitVo.setCityName(cityInfo.getName());

        Long districtId = apartmentSubmitVo.getDistrictId();
        DistrictInfo districtInfo = districtInfoService.getById(districtId);
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //必须执行新增动作
        //1.新增公寓信息  应该是公寓对象  但是  vo继承公寓对象
        this.saveOrUpdate(apartmentSubmitVo);
        //数据新增 主键自动回显!!!
        long id = apartmentSubmitVo.getId();

        //2.新增图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
        for (GraphVo graphVo : graphVoList){
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemId(id);
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfoList.add(graphInfo);
        }
        //提供了批处理 只有通用service有批处理
        graphInfoService.saveBatch(graphInfoList);

        //3.入库公寓配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
        for(long facilityInfoId : facilityInfoIds){
            ApartmentFacility apartmentFacility =ApartmentFacility.builder()
                    .apartmentId(id)
                    .facilityId(facilityInfoId).build();
            apartmentFacilityList.add(apartmentFacility);
        }
        apartmentFacilityService.saveBatch(apartmentFacilityList);

        //4.新增标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
        for(long labelId : labelIds){
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(id).labelId(labelId).build();
            apartmentLabelList.add(apartmentLabel);
        }
        apartmentLabelService.saveBatch(apartmentLabelList);

        //5.新增杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
        for(long feeValueId : feeValueIds){
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(id).feeValueId(feeValueId).build();
            apartmentFeeValueList.add(apartmentFeeValue);
        }
        apartmentFeeValueService.saveBatch(apartmentFeeValueList);
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //1.查询公寓信息
        ApartmentInfo apartmentInfo = this.getById(id);
        //将2个对象相同的数据  getxxx()    setxxx()
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        //3.获取图片列表
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemId,id)
                         .eq(GraphInfo::getItemType,ItemType.APARTMENT);
        List<GraphInfo> infoList = graphInfoService.list(graphQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>(infoList.size());
        for (GraphInfo graphInfo : infoList){
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVoList.add(graphVo);
        }
        apartmentDetailVo.setGraphVoList(graphVoList);

        //4.获取标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);

        //5.获取配套列表
        List<FacilityInfo> facilityInfoList =facilityInfoMapper.selectByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        //6.获取杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

    @Override
    @Transactional
    public void deleteById(Long id) {

        //1.在删除公寓的时候应该先检查是否有房间  根据公寓的id号查询房间的数量
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(queryWrapper);

        if(count > 0){
            //不让删除!!!!!
            throw new LeaseException(ResultCodeEnum.ADMIN_HAS_ROOM);
        }


        //1.1删除图片信息.
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphInfoQueryWrapper);

        //1.2 删除公寓配套信息
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

        //1.3删除公寓和标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelQueryWrapper);

        //1.4删除杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);

        this.removeById(id);
    }
}




